package tgc.edu.fhh.web.controller;

import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.fhh.custom.AjaxResult;
import tgc.edu.fhh.custom.TablePageable;
import tgc.edu.fhh.entity.Course;
import tgc.edu.fhh.entity.Grade;
import tgc.edu.fhh.entity.Student;
import tgc.edu.fhh.entity.SysUser;
import tgc.edu.fhh.entity.TbClass;
import tgc.edu.fhh.security.UserUtils;
import tgc.edu.fhh.service.CourseService;
import tgc.edu.fhh.service.GradeService;
import tgc.edu.fhh.service.StudentService;
import tgc.edu.fhh.service.TbClassService;

@Controller
@RequestMapping(value = "/grade")
public class GradeController {
	@Autowired
	private GradeService gradeService;
	@Autowired
	private TbClassService classService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private  CourseService courseService;
	@Autowired
	private UserUtils userUtils;
	
	@RequestMapping(value = "/peasonGrade")
	public String peasonGrade(ModelMap modelMap) {
		SysUser user = userUtils.getUser();
		Integer id = user.getId();
		List<Grade> list = gradeService.findByStudentId(id);
		modelMap.put("list", list);
		return "grade/peasonGrade";
	}
	//----------获取班级名单---------
	@RequestMapping(value = "/grcjlr")
	public void grcjlr(ModelMap modelMap) {
		List<TbClass> list = classService.findAll();
		modelMap.put("bjs", list);
	}
	//--------------------根据班级id获取学生--------------
	@RequestMapping(value = "/getStu")
	@ResponseBody
	public Object getStu(Integer bjId,ModelMap modelMap) {
		List<Student> students=studentService.findByTbClassId(bjId);
		return students;
	}
	//-------------获取学生成绩------------------
	@RequestMapping(value = "/getgrcj")
	@ResponseBody
	public Object getgrcj(Integer stuId,ModelMap modelMap) {
		List<Grade> grades=gradeService.findByStudentId(stuId);
		return grades;
	}
	@RequestMapping(value = "/grcjcx")
	public String grcjcx(ModelMap modelMap) {
		SysUser user=userUtils.getUser();
		Integer id=user.getId();
		List<Grade> list=gradeService.findByStudentId(id);
		modelMap.put("list", list);
		return "grade/grcjcx";
	}
	//----------------修改成绩------------
	@RequestMapping(value = "/xgcj")
	@ResponseBody
	public void xgcj(Integer id,Integer grade) {
		Grade g=gradeService.findById(id);
		g.setGrade(grade);
		gradeService.save(g);
	}

	@RequestMapping(value = "/xgcj1")
	@ResponseBody
	public void xgcj1(Integer id,Integer grade) {
		Grade g1=gradeService.findById(id);
		g1.setGrade(grade);
		gradeService.save(g1);
	}
	@RequestMapping(value = "/xgcj3")
	public String xgcj3(Integer bjId,ModelMap modelMap) {
		List<Course> courses=gradeService.findCourseByTbClassId(bjId);
		List<List<Object>> cjb= gradeService.getCjb(bjId,courses);
		modelMap.put("kcs", courses);
		modelMap.put("cjb", cjb);
		return "grade/cjb";
	}
//	-------------------
	//----------------------------获取学生名单-----------------------------------------------
	@RequestMapping(value = "/bjcjlr")
	public void bjcjlr(ModelMap modelMap) {
		List<TbClass> list = classService.findAll();
		modelMap.put("bjs", list);
	}

//  班级成绩查询
@RequestMapping(value = "/cjcx")
public String bjcjcx(ModelMap modelMap) {
List<TbClass> list=classService.findAll();
modelMap.put("list", list);
return "grade/cjcx";
}
//--------------------------------------------------------------
//-------------------------------------------------------------------------------
@SuppressWarnings("serial")
public Specification<Grade> buildSpec(Integer bjId,Integer kcId) {
	Specification<Grade> spec=new Specification<Grade>() {

		@Override
		public Predicate toPredicate(Root<Grade> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			HashSet<Predicate> rules=new HashSet<>();
			if(bjId!=null) {
				rules.add(cb.equal(root.get("student").get("tbClass").get("id").as(String.class), bjId));
			}
			if(kcId!=null) {
				rules.add(cb.equal(root.get("course").get("id").as(String.class), kcId));
			}
			return cb.and(rules.toArray(new Predicate[rules.size()]));
		}
	};
	return spec;
}
@RequestMapping(value = "/page")
@ResponseBody
public Object page(TablePageable tablePaegable,Integer bjId,Integer kcId) {
	List<Student> students=studentService.findByTbClassId(bjId);
	for(Student s:students) {
	Grade grade=gradeService.findByStudentIdAndCourseId(s.getId(), kcId);
	if(grade==null) {
		Grade grade1=new Grade();
		grade1.setCourse(courseService.findById(kcId));
		grade1.setStudent(studentService.findById(s.getId()));
		grade1.setGrade(null);
		gradeService.save(grade1);
		}
	}
	PageRequest pageable = tablePaegable.buildPageRequest();
	Specification<Grade> spec = buildSpec(bjId,kcId);
	Page<Grade> page = gradeService.findAll(spec, pageable);
	return AjaxResult.buildTableResult(page);
}


}

